package com.rambo.baselib.util;

import com.rambo.baselib.ResourceTable;
import com.rambo.baselib.provider.ListItemProvider;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

public class Loading extends ListContainer {

    public enum LoadingState {
        LOADING, SUCCESS, FAIL, EMPTY
    }

    private StateAdapter stateAdapter;

    public Loading(Context context) {
        super(context);
//        init();
    }

    public Loading(Context context, AttrSet attrSet) {
        super(context, attrSet);
//        init();
    }

    public Loading(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
//        init();
    }
    private Component contentComponent;
    public void init( Component contentComponent) {
        this.contentComponent=contentComponent;
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(RgbColor.fromArgbInt(0x000000));
        setBackground(shapeElement);
        setVisibility(Component.HIDE);
        stateAdapter = new StateAdapter(null);
        stateAdapter.replace(LoadingState.LOADING);
        setItemProvider(stateAdapter);

    }
    private class StateAdapter extends BaseItemProvider {

        private List<LoadingState> states;

        public StateAdapter(List<LoadingState> states) {
            this.states = states == null ? new ArrayList<>() : states;
        }

        @Override
        public int getCount() {
            return states.size();
        }

        public void replace(LoadingState loadingState) {
            states.clear();
            states.add(loadingState);
            notifyDataChanged();
        }
        public void clear() {
            states.clear();
            notifyDataChanged();
        }

        @Override
        public LoadingState getItem(int position) {
            return states.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
            LoadingState loadingState = getItem(position);
            if (component == null) {
                LogUtil.e("==="+loadingState);
                int resId = ResourceTable.Layout_componet_loading;
                if (loadingState == LoadingState.FAIL) {
                    resId = ResourceTable.Layout_componet_loading_fail;
                } else if (loadingState == LoadingState.EMPTY) {
                    resId = ResourceTable.Layout_componet_loading_empty;
                }
                component = LayoutScatter.getInstance(componentContainer.getContext()).parse(resId, null, false);
                component.setTag(new Holder(component));
            }else {

            }
            Holder holder = (Holder) component.getTag();
            return component;
        }
    }

    private class Holder {

        public Component rootComponent;

        public Holder(Component rootComponent) {
            this.rootComponent = rootComponent;
        }
    }

    public void updateLoadState(LoadingState state) {
        LogUtil.e("==updateLoadState="+state);
        if (state == LoadingState.SUCCESS) {
            setVisibility(Component.HIDE);
            contentComponent.setVisibility(Component.VISIBLE);
            stateAdapter.clear();
            return;
        }
        contentComponent.setVisibility(Component.HIDE);
        setVisibility(Component.VISIBLE);
        stateAdapter.replace(state);
    }
}
